package com.kksoft.readcm.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kksoft.readcm.a.Appconfig;
import com.kksoft.readcm.pro.R;

public class BasicWebViewActivity extends RilAppActivity
{
  public void onCreate(Bundle bundle)
  {
    //this.t = 1;
    super.onCreate(bundle);
    if(getIntent().getStringExtra("pathToLoad") == null)
    {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    } else
    {
        WebView webview = (WebView)findViewById(R.id.basic_webview);
        webview.setWebViewClient(new WebViewClient(){

			/* (non-Javadoc)
			 * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
			 */
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				boolean flag;
		        if(url.startsWith("browser::"))
		        {
		            Uri uri = Uri.parse(url.replaceFirst("browser::", ""));
		            Intent intent = new Intent("android.intent.action.VIEW", uri);
		            startActivity(intent);
		            flag=true;
		        } else
		        if(url.startsWith("webview::"))
		        {
		            Intent intent1 = new Intent(BasicWebViewActivity.this, BasicWebViewActivity.class);
		            String s1 = url.replaceFirst("webview::", "");
		            Intent intent2 = intent1.putExtra("pathToLoad", s1);
		            startActivity(intent1);
		            flag=true;
		        } else
		        if(url.startsWith("mailto:"))
		        {
		        	Intent it=new Intent("android.intent.action.SEND");
		            Intent intent3 = it.setType("plain/text");
		            String as[] = new String[1];
		            String s2 = url.replaceFirst("mailto:", "");
		            as[0] = s2;
		            it.putExtra("android.intent.extra.EMAIL", as);
		            if(App.a(BasicWebViewActivity.this, it))
		                startActivity(it);
		            flag=true;
		        } else
		        {
		        	flag=false;
		        }
		        return flag;
			}
        	
        });

        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.setScrollBarStyle(0);
        webview.setBackgroundColor(0);
        webview.loadUrl(getIntent().getStringExtra("pathToLoad"));
    }
  }

  public boolean onPrepareOptionsMenu(Menu menu)
  {
	  boolean flag;
      if(Appconfig.getThis().isLogined())
      {
          flag = super.onPrepareOptionsMenu(menu);
      } else
      {
          menu.setGroupVisible(-1, false);
          menu.setGroupVisible(-1, false);
          menu.setGroupVisible(-1, false);
          flag = true;
      }
      return flag;
  }

  protected final int getLayoutID()
  {
    return R.layout.activity_basic_webview;
  }
}